/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Modulo4;

import Modelo.DAL.ReporteObjetoPerdido_DAO;
import Acceso.Security;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;
import org.json.JSONObject;

/**
 *
 * @author Rafael
 */
public class consultarEntregaOPFormAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {

        String[] a = {"Administrador", "Director DSI", "Jefe Apoyo Administrativo", "Personal Administrativo","Encargado Objetos Perdidos"};
       if (!Security.validateSession(request, a)) {
            return mapping.findForward("errorPriv");
        }
        System.out.println("Paso Autenticación");

        consultarEntregaOPForm formBean = (consultarEntregaOPForm) form;
        ActionErrors errors = new ActionErrors();

        String tipoObjeto = request.getParameter("tipoObjeto");
        String fechaDesde = request.getParameter("fechaDesde");
        String fechaHasta = request.getParameter("fechaHasta");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        String where = "";

        System.out.println("\n\n Tipo Objeto: " + tipoObjeto);
        System.out.println("\n\n desde " + fechaDesde);
        System.out.println("\n\n hasta " + fechaHasta);

        if (!limit.equals("0")) {
            where += " limit " + limit + " offset " + offset;
        }

        response.setContentType("text/json");
        JSONObject personas = ReporteObjetoPerdido_DAO.consultarEOP(where, tipoObjeto, fechaDesde, fechaHasta);
        if (personas == null) {
            errors.add("consulta_ReporteVacia", new ActionMessage("errors.consulta_ReporteVacia"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }

        JSONObject ext = new JSONObject();
        ext.put("VerNomina", personas);
        PrintWriter pw = response.getWriter();
        pw.write(ext.toString());
        pw.flush();
        pw.close();

        return mapping.findForward(SUCCESS);
    }
}
